<?php
    // only used to process forms registered
    // in the admin section
    class Intake_Controller extends Application {
        var $do_save_inquiry       = true;
        var $do_inquiry_notify     = true;
        var $do_mail_autoresponder = true;
        var $do_redirect           = true;
        
        function setup()
        {
            $this->actions = array('index','thank_you');
            use_model('inbox','inbox_leads');
        }
        
        // process the intake and either redirect
		function index() 
		{
		    $this->form_errors = array();
		    if ( !$_POST || empty($_POST['form_id']) )
		    {
		        $this->form_errors['Empty Form'] = array('No valid form was posted.');
		        $this->assign('errors',$this->form_errors);
		        return;
		    }

		    // proccess form
		    $clean = clean_variables_for_mail($_POST);
		    $this->assign('fields',$clean);		    
		    
		    // compile msg
		    $email_msg = $this->render( array('partial'=>'_msg.php','string'=>true) );

		    // get form details
		    $inbox       = new Inbox();
		    $redirect    = '';		    
		    if ( $inbox->load((int) $clean['form_id']) )
		    {		  
		        
		        $redirect = $inbox->get('form_redirect');
		        do {    		        
		            // validate email
        		    if (!is_valid_email($clean['email'])) {
        		        $this->form_errors['Invalid Email'] = array('Your email is invalid');
        		        break;
    		        }
    		        
    		        // run intake plugins
    		        $plugins = $inbox->get('plugins');
    		        if ( !empty($plugins) )
    		        {
    		            $un = unserialize($plugins);
    		            foreach($un as $p){
                            $this->plugin($p,$clean);   
                        }
                        if(count($this->form_errors)>0) break;
    		        }
		            
		            // save lead
		            if ($this->do_save_inquiry) // plugin can override this.
		            {
        		        $inbox_leads = new Inbox_Leads();
            		    $inbox_leads->set('inbox_form_id',$clean['form_id']);
            		    $inbox_leads->set('email',$clean['email']);
            		    $inbox_leads->set('first_name',$clean['first_name']);
            		    $inbox_leads->set('last_name',$clean['last_name']);
            		    $inbox_leads->set('phone',$clean['phone']);
            		    $inbox_leads->set('form_message',$email_msg);
            		    $inbox_leads->save();		    
            		    $this->form_errors = $inbox_leads->get_errors();
                        if(count($this->form_errors)>0) break;
                    }
                    
                    // send out mail to thank user only if autoresponder 
                    // has been enabled
                    use_pixel('ActionMailer.php');
                    if($this->do_mail_autoresponder) // plugin can override this
                    {
                        $autoresponder = $inbox->get('autoresponder');
                        if ( !empty( $autoresponder ) 
                             && $inbox->get('disable_autoresponder') != 1 )
                        {                        
                            $autoresponder_subject = $inbox->get('autoresponder_subject');
                            $autoresponder_subject = !empty($autoresponder_subject) ?
                                                     $autoresponder_subject :
                                                     'Thank you for contacting us';
                            $clean['form_name']    = $inbox->get('form_name');
                        
                            // do form var substitution for autoresponder
                            foreach( $clean as $key => $value )
                            {
                                $autoresponder = str_replace("{\$$key}",$value,$autoresponder);
                            }
                        
                            $mail =& new ActionMailer();
                            $mail->AddReplyTo('noreply@'.$_SERVER['SERVER_NAME']);
                            $mail->Subject  = $autoresponder_subject;
                            $mail->Body     = $autoresponder;
                            $mail->AddAddress($clean['email'], $clean['first_name'].' '.$clean['last_name']);
                            if ( !$mail->Send() ) error_log('E-mail could not be sent to: '.$clean['email'].' '.$mail->ErrorInfo);                    
                            unset($mail);                        
                        }
                    } 
                    
                    //  notify system
                    if ($this->do_inquiry_notify) // plugin can override this
                    {
                        $to = $inbox->get('form_to');
                        if ( !empty( $to ) )
                        {
                            $mail =& new ActionMailer();
                            $mail->AddReplyTo($clean['email'],$clean['first_name'].' '.$clean['last_name']);
                            $mail->From     = $clean['email'];
                            $subject        = $inbox->get('subject');
                            $mail->Subject  = empty($subject) ? 'New Website Contact' : $subject; 
                            $mail->Body     = $email_msg;
                            $send_to = array('form_to'=>'AddAddress','form_cc'=>'AddCC','form_bcc'=>'AddBCC');
                            foreach( $send_to as $key => $value )
                            {
                                $emails = explode(',',$inbox->get($key));
                                foreach($emails as $email) {
                                    $mail->$value($email);
                                }
                            }                        
                            if ( !$mail->Send() ) error_log('E-mail could not be sent'.$mail->ErrorInfo);    
                            unset($mail);
                        }
                    }                   

        		    // put stuff in flash
        		    flash('name', $clean['first_name'].' '.$clean['last_name']);
		        } while(false);
		    }		    		    		    
		    
		    // all done		    
		    $this->assign('errors',$this->form_errors);
		    $redirect = !empty($redirect) ? $redirect : 'thank_you';
		    if ($this->do_redirect)
		    {
		        if (empty($this->form_errors)) $this->redirect_to($redirect);
	        }
		}
		
		function thank_you(){}

        
    }

?>